package playing;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PlantLogin {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/ZUT";
    private static final String USER = "root";
    private static final String PASS = "123456";

    // 删除用户
    public boolean deleteUser(String username) {
        String sql = "DELETE FROM users WHERE username = ?";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, username);

            int rowsDeleted = pstmt.executeUpdate();
            return rowsDeleted > 0; // 如果删除了行，则返回true，否则返回false

        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 如果发生错误，则返回false
        }
    }

    // 注册新用户并返回自动分配的用户编号
    public int register(String username, String password) {
        String sql = "INSERT INTO users (username, password) VALUES (?, ?)";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            pstmt.setString(1, username);
            pstmt.setString(2, password); // 注意：这里应该使用加密后的密码

            int rowsInserted = pstmt.executeUpdate();
            if (rowsInserted > 0) {
                try (ResultSet rs = pstmt.getGeneratedKeys()) {
                    if (rs.next()) {
                        // 这里返回的是users表的idf
                        System.out.println(rs.getInt(1));
                        return rs.getInt(1);
                    }
                }
            }
            return -1; // 如果没有插入或获取不到ID，则返回-1

        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    // 用户登录并返回人物编号
    public int login(String username, String password) {
        String sql = "SELECT users_id FROM users WHERE username = ? AND password = ?";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, username);
            pstmt.setString(2, password); // 注意：这里应该使用加密后的密码进行验证

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    //System.out.println(rs.getInt("users_id"));
                    return rs.getInt("users_id");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1; // 如果没有找到用户或密码不正确，则返回-1
    }
    //获取所有用户的id
    public List<Integer> getAllUserIds() {
        List<Integer> userIds = new ArrayList<>();
        String sql = "SELECT users_id FROM users"; // 假设用户ID列是users_id

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                int userId = rs.getInt("users_id");
                userIds.add(userId);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userIds;
    }

    //查找用户
    public boolean userExistsById(int userId) {
        String sql = "SELECT COUNT(*) FROM users WHERE users_id = ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, userId);

            // 执行查询并获取结果
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    // 如果COUNT(*)返回大于0，则表示用户存在
                    return rs.getInt(1) > 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 你可以在这里添加更详细的错误处理逻辑
        }

        // 如果出现异常或未找到用户，则返回false
        return false;
    }


}